#include "flayscene.h"
#include"qdebug.h"
#include"qmenubar.h"
#include"mypushbutton.h"
#include"qpushbutton.h"
#include"qlabel.h"
#include"qpainter.h"
#include"mycoin.h"
#include"dataconfig.h"
FlayScene::FlayScene(int i)
{
    this->levelIndex=i;
    qDebug()<<"in "<<i<<endl;

    QString str = QString("level %1").arg(i);
    // 1. 设置窗口信息
    this->setFixedSize(420, 680);
    this->setWindowTitle(str);
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));

    // 2. 创建菜单栏
    QMenuBar* menubar =  menuBar();
    setMenuBar(menubar);

    // 3. 创建菜单栏开始
    QMenu* startMenu = menubar->addMenu("Start");

    // 4. 创建菜单项退出
    QAction* quitAction = startMenu->addAction("Exit");

    // 5. 退出
    connect(quitAction, &QAction::triggered, [=](){
        this->close();
    });
    MyPushButton* backBtn = new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
        backBtn->setParent(this);
    //    backBtn->resize(80,80);
    //    backBtn->move(320,620);
        backBtn->move(this->width()-backBtn->width()-20, this->height()-backBtn->height()-20);
        connect(backBtn, &QPushButton::clicked, [=](){
            qDebug() << "back";
            emit this->selectLevelScene();
        });
        QLabel* label = new QLabel;
           label->setParent(this);

           QFont font;
           font.setFamily("楷体");
           font.setPointSize(26);
           label->setFont(font);

           QString labelText = QString("Level:%1").arg(levelIndex);
           label->setText(labelText);

           label->setGeometry(30, this->height()-66, 200, 50);
           dataConfig config;
           for(int i = 0; i < 4; i++){
                 for(int j = 0; j < 4; j++){
                     this->gamearray[i][j] = config.mData[this->levelIndex][i][j];
                 }
             }
           for(int i = 0; i < 4; i++){
                   for(int j = 0; j < 4; j++){
                       QPixmap pix = QPixmap(":/res/BoardNode.png");
           //            pix.scaledToWidth(pix.width()*2);
           //            pix.scaledToHeight(pix.height()*2);

                       QLabel* label = new QLabel;
                       label->setScaledContents(true);
                       label->setGeometry(0,0,pix.width()*1.2,pix.height()*1.2);

                       label->setPixmap(pix);
                       label->setParent(this);
                       label->move(80+i*60,200+j*60);

                       QString str;
                       if(this->gamearray[i][j]==1)
                       {
                           str=":/res/Coin0001.png";
                       }
                       else
                       {
                            str=":/res/Coin0008.png";
                        }
                       MyCoin * coin=new MyCoin(str);
                       coin->setParent(this);
                       coin->move(90+i*60,210+j*60);
                       coin->posX=i;
                       coin->poxY=j;
                       coin->flag=this->gamearray[i][j];
                       coinBtn[i][j]=coin;
                       connect(coin,&MyCoin::clicked,[=](){
                           qDebug()<<"paly jin";
                           coin->changFlg();
                           this->gamearray[i][j] = this->gamearray[i][j] == 0 ? 1 : 0;

                                        if(coin->poxY-1 >= 0)
                                        {
                                             coinBtn[coin->posX][coin->poxY-1]->changFlg();
                                             this->gamearray[coin->posX][coin->poxY-1] = this->gamearray[coin->posX][coin->poxY-1] == 0 ? 1 : 0;
                                         }

                                         //下
                                         if(coin->poxY+1 <= 3){
                                             coinBtn[coin->posX][coin->poxY+1]->changFlg();
                                             this->gamearray[coin->posX][coin->poxY+1] = this->gamearray[coin->posX][coin->poxY+1] == 0 ? 1 : 0;
                                         }

                                         //左
                                         if(coin->posX-1 >= 0){
                                             coinBtn[coin->posX-1][coin->poxY]->changFlg();
                                             this->gamearray[coin->posX-1][coin->poxY] = this->gamearray[coin->posX-1][coin->poxY] == 0 ? 1 : 0;
                                         }

                                         //右
                                         if(coin->posX+1 <= 3){
                                             coinBtn[coin->posX+1][coin->poxY]->changFlg();
                                             this->gamearray[coin->posX+1][coin->poxY] = this->gamearray[coin->posX+1][coin->poxY] == 0 ? 1 : 0;
                                         }
                                         this->win=true;
                                         for(int i = 0; i < 4; i++){
                                                 for(int j = 0; j < 4; j++){
                                                     if(coinBtn[i][j]==false)
                                                        {
                                                         win=false;
                                                         break;
                                                     }
                                                 }
                                         }
                                         if(win){
                                             qDebug()<<"win";
                                         }

                       });
                   }
           }
}

void FlayScene::paintEvent(QPaintEvent *event){
    // 1. 加载背景图片
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");

    // 2. 创建画家
    QPainter painter(this);
    painter.drawPixmap(0, 0, this->width(), this->height(), pix);

    // 3. 加载游戏图标
    pix.load(":/res/Title.png");
    pix = pix.scaled( pix.width()*0.8, pix.height()*0.8);
    painter.drawPixmap(10, 30, pix);
}
